import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:juming_test/module/domain/domain_filter/domain_filter_logic.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_conditions.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_main_list.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_sub_list.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_tool_bar.dart';

@RoutePage()
class DomainFilterScreen extends StatefulWidget implements AutoRouteWrapper {
  const DomainFilterScreen({super.key, @queryParam this.data = ''});
  // 页面传参
  final String data;

  @override
  State<DomainFilterScreen> createState() => _DomainFilterScreenState();

  @override
  Widget wrappedRoute(BuildContext context) {
    final DomainFilterLogic logic = Get.put(DomainFilterLogic());
    logic.paramsData = data;
    return this;
  }
}

class _DomainFilterScreenState extends State<DomainFilterScreen> {
  final DomainFilterLogic _domainFilterLogic = Get.put(DomainFilterLogic());

  @override
  void dispose() {
    super.dispose();
    Get.delete<DomainFilterLogic>();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CupertinoNavigationBar(
        middle: const Text('高级筛选'),
        leading: IconButton(
          onPressed: () {
            context.pop();
          },
          icon: Icon(Icons.arrow_back_ios, size: 20.w),
        ),
        backgroundColor: Colors.white,
      ),
      body: Container(
        color: Colors.grey[200],
        child: Row(
          children: [
            SizedBox(
              width: 100.w,
              child: Obx(() {
                final sections =
                    _domainFilterLogic.domainFilterViewModel.sections.value;
                final selectedIndex = _domainFilterLogic.selectedIndex.value;
                // 左侧标题列表
                return DomainFilterMainList(
                  sections: sections,
                  selectedIndex: selectedIndex,
                  onSelectedIndexChanged:
                      (index) => _domainFilterLogic.updateSelectedIndex(index),
                );
              }),
            ),
            const SizedBox(width: 5),
            Expanded(
              child: Container(
                color: Colors.white,
                child: Obx(() {
                  final sections =
                      _domainFilterLogic.domainFilterViewModel.sections.value;
                  final selectedIndex = _domainFilterLogic.selectedIndex.value;
                  if (selectedIndex < 0 || selectedIndex >= sections.length) {
                    return const SizedBox();
                  }
                  // 右侧内容列表
                  return DomainFilterSubList(
                    section: sections[selectedIndex],
                    onValueChanged: (key, value) {
                      // 用户操作
                      _domainFilterLogic.domainFilterViewModel.updateFilterData(
                        key,
                        value,
                      );
                    },
                  );
                }),
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: Obx(() {
        final conditionItems =
            _domainFilterLogic.domainFilterViewModel.conditionItems.value;
        return DomainFilterToolBar(
          filterCount: conditionItems.length,
          onCheck: () {
            // 查看
            _showFilterDialog(context);
          },
          onReset:
              () => _domainFilterLogic.domainFilterViewModel.resetFilterData(),
          onConfirm: () {
            // 确认
            _domainFilterLogic.confirmFilter();
          },
        );
      }),
    );
  }

  // 弹框筛选条件
  void _showFilterDialog(BuildContext context) {
    if (_domainFilterLogic.domainFilterViewModel.conditionItems.value.isEmpty) {
      Fluttertoast.showToast(
        msg: '请先添加条件',
        gravity: ToastGravity.CENTER,
        backgroundColor: Colors.black87,
      );
      return;
    }
    showDialog(
      context: context,
      builder: (context) {
        return Obx(() {
          final conditionItems =
              _domainFilterLogic.domainFilterViewModel.conditionItems.value;
          if (conditionItems.isEmpty) {
            context.pop();
          }
          return Dialog(
            child: Container(
              width: double.infinity,
              padding: EdgeInsets.all(16.w),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8.w),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '已添加的条件${conditionItems.length}',
                        style: TextStyle(
                          fontSize: 16.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(
                        width: 25.w,
                        height: 25.w,
                        child: IconButton(
                          padding: EdgeInsets.zero,
                          constraints: const BoxConstraints(),
                          onPressed: () {
                            context.pop();
                          },
                          icon: const Icon(Icons.close, color: Colors.white),
                          iconSize: 20.w,
                          style: IconButton.styleFrom(
                            backgroundColor: Colors.grey,
                            shape: const CircleBorder(),
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 16.w),
                  SizedBox(
                    width: double.infinity,
                    child: DomainFilterConditions(
                      list: conditionItems,
                      onDelete: (index) {
                        _domainFilterLogic.domainFilterViewModel
                            .deleteFilterItem(index);
                      },
                    ),
                  ),
                ],
              ),
            ),
          );
        });
      },
    );
  }
}
